package com.bishe.service;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.api.EcoBootException;
import com.api.entitis.Resume;
import com.api.entitis.User;
import com.api.entitis.model.LoginModel;
import com.api.entitis.model.UserModel;
import com.api.entitis.query.UserQuery;
import com.api.service.IResumeService;
import com.api.service.IUserService;
import com.api.util.UserStpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 *
 * @date 2022/2/15 14:17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private IResumeService resumeService;
    @Override
    public UserModel login(LoginModel loginModel) {
        //用户名
        String userName = loginModel.getUserName();
        //密码
        String password = loginModel.getPassword();
        //根据用户名密码查找
        User one = getOne(new LambdaQueryWrapper<User>()
                        .eq(User::getUserName, userName)
                        .eq(User::getPassWord, password)
                , false
        );
        //记住我
        Boolean rememberMe = loginModel.getRememberMe();
        //默认 不记住我
        if (rememberMe == null){
            rememberMe = false;
        }
        if (one != null){
            String id = one.getId();
            Resume resume = resumeService.getOne(new LambdaQueryWrapper<Resume>().eq(Resume::getCreateBy, id));
            // 在登录时写入当前会话的账号id 并且设置记住我功能
            UserStpUtil.login(one.getId(), rememberMe);
            UserModel userModel = new UserModel();
            userModel.setId(one.getId());
            userModel.setName(one.getUserName());
            userModel.setType("user");
            if (resume != null){
                userModel.setTypeName(resume.getType());
            }
            userModel.setToken(UserStpUtil.getTokenValue());
            return userModel;
        }

        return null;
    }

    @Override
    public void registerOrUpdate(User user) {
        String userName = user.getUserName();
        //根据用户名查找
        User one = getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUserName, userName),
                false
        );

        //用户名不存在 为注册
        if (one == null){
            //设置一个不重复的id
            user.setId(UUID.randomUUID().toString());
            //设置创建时间
            user.setCreateTime(new DateTime());
            //保存数据到数据库
            save(user);
            return;
        }

        //存在为修改
        //检查是否登陆
        boolean login = UserStpUtil.isLogin();
        if (login){
            //登陆了  查看是否是修改当前登陆人
            String loginId = (String) UserStpUtil.getLoginId();
            if (one.getId().equals(loginId)){ //是当前登陆人
                //创建时间和用户名不可修改
                user.setCreateTime(one.getCreateTime());
                user.setUserName(one.getUserName());
                //修改
                updateById(user);
                return;
            }

            //不是当前登陆人
            throw new EcoBootException("没有权限修改！");
        }
    }

    @Override
    public IPage<User> toPage(UserQuery query) {
        // 查询条件构造器
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isEmpty(query.getUserName())){
            wrapper.like(User::getUserName,query.getUserName());
        }
        // 根据时间倒叙
        wrapper.orderByAsc(User::getCreateTime);

        IPage<User> page = this.page(query.toPage(), wrapper);

        return page;
    }

    @Override
    public User getUser(String id) {

        User user = getById(id);
        if (user != null){
            return user;
        }
        return null;
    }

}
